| Conditions | 25 |
| Total Lines | 153 |
| Code Lines | 70 |
| Lines | 153 |
| Ratio | 100 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like directive.upload.js ➔ UploadConstructor often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | // JavaScript Document |
||
| 12 | function UploadConstructor($scope, $element, $attrs) { |
||
| 13 | var constructor = this; |
||
|
|
|||
| 14 | var $ctrl = $scope.uploadCtrl; |
||
| 15 | var tagName = $element[0].tagName.toLowerCase(); |
||
| 16 | $scope.DisplayMessageList = MessageService.messageList; |
||
| 17 | |||
| 18 | function TryToCallInitDirective(){ |
||
| 19 | if(typeof $scope.InitDirective == "function"){ |
||
| 20 | $scope.InitDirective($scope, $element, $attrs, $ctrl); |
||
| 21 | }else{ |
||
| 22 | $scope.DefaultInitDirective(); |
||
| 23 | } |
||
| 24 | } |
||
| 25 | $scope.DefaultInitDirective = function(){ |
||
| 26 | if(Core.GetConfig().debugLog.DirectiveFlow) |
||
| 27 | console.log("scope.$id:"+$scope.$id+", may implement $scope.InitDirective() function in webapge"); |
||
| 28 | } |
||
| 29 | function InitializeUpload() { |
||
| 30 | $scope.uploadInfo = []; |
||
| 31 | $scope.uploadResult = []; |
||
| 32 | |||
| 33 | $scope.uploadInstance = null; |
||
| 34 | } |
||
| 35 | |||
| 36 | function UploadFileList(files){ |
||
| 37 | var isFiles = Array.isArray(files); |
||
| 38 | |||
| 39 | $scope.uploadInfo = []; |
||
| 40 | |||
| 41 | if(!isFiles){ |
||
| 42 | UploadFile(files); |
||
| 43 | } |
||
| 44 | else{ |
||
| 45 | for(var index in files){ |
||
| 46 | UploadFile(files[index]); |
||
| 47 | } |
||
| 48 | } |
||
| 49 | } |
||
| 50 | |||
| 51 | function UploadFile(file, options, callback) { |
||
| 52 | var url = $rootScope.serverHost; |
||
| 53 | var uploadInfoRecord = {}; |
||
| 54 | var recordCount = $scope.uploadInfo.length; |
||
| 55 | // create new row in uploadInfo, since upload in async |
||
| 56 | $scope.uploadInfo[recordCount] = {}; |
||
| 57 | |||
| 58 | if (!file || file.$error) { |
||
| 59 | return; |
||
| 60 | } |
||
| 61 | |||
| 62 | uploadInfoRecord.fileInfo = file; |
||
| 63 | uploadInfoRecord.uploadResult = {}; |
||
| 64 | |||
| 65 | uploadInfoRecord.name = file.name; |
||
| 66 | uploadInfoRecord.size = file.size; |
||
| 67 | uploadInfoRecord.uploadProgress = 0; |
||
| 68 | |||
| 69 | // File Object |
||
| 70 | // console.dir(file) |
||
| 71 | /* |
||
| 72 | lastModified: 1474968722283 |
||
| 73 | lastModifiedDate: Tue Sep 27 2016 17:32:02 GMT+0800 (China Standard Time) |
||
| 74 | name: "hu01ca.xlsx" |
||
| 75 | size: 8629 |
||
| 76 | type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" |
||
| 77 | upload: d |
||
| 78 | webkitRelativePath: "" |
||
| 79 | */ |
||
| 80 | |||
| 81 | // Upload Result from PHP |
||
| 82 | /* |
||
| 83 | { |
||
| 84 | "name": "hu01ca.xlsx", |
||
| 85 | "type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", |
||
| 86 | "tmp_name": "D:\\xampp\\tmp\\phpDFD9.tmp", |
||
| 87 | "error": 0, |
||
| 88 | "size": 8629, |
||
| 89 | "movedTo": "D:\\xampp\\htdocs\\Develop\\model/../temp/upload/hu01ca.xlsx", |
||
| 90 | "fileIntegrity-md5": "3e7992dbabfbc9ea84c621762831975b", |
||
| 91 | "fileIntegrity-sha1": "691528b6437c8e686d342eeacd0f27620a6ba295", |
||
| 92 | "errorMsg": "" |
||
| 93 | } |
||
| 94 | */ |
||
| 95 | |||
| 96 | var uploadAction = $scope.uploadInstance = Upload.upload({ |
||
| 97 | //url: 'https://angular-file-upload-cors-srv.appspot.com/upload', |
||
| 98 | url: url+'/controller/documentUploader.for12.2.21.php', |
||
| 99 | data: {file: file}, |
||
| 100 | }); |
||
| 101 | // http://api.jquery.com/deferred.then/#deferred-then-doneCallbacks-failCallbacks |
||
| 102 | // deferred.then( doneCallbacks, failCallbacks [, progressCallbacks ] ) |
||
| 103 | uploadAction.then(function (response) { |
||
| 104 | uploadInfoRecord.uploadResult = response.data; |
||
| 105 | //if(response.data.error) |
||
| 106 | //$scope.errorMsg = response.data.error + " - "+response.data.errorMsg |
||
| 107 | |||
| 108 | $scope.uploadInfo[recordCount] = uploadInfoRecord; |
||
| 109 | // $ctrl.ngModel = $scope.uploadInfo; |
||
| 110 | |||
| 111 | $scope.uploadResult[$scope.uploadResult.length] = response.data; |
||
| 112 | $ctrl.ngModel = $scope.uploadResult; |
||
| 113 | }, function (response) { |
||
| 114 | //if(response.data.error) |
||
| 115 | //$scope.errorMsg = response.data.error + " - "+response.data.errorMsg |
||
| 116 | }, function (evt) { |
||
| 117 | // Math.min is to fix IE which reports 200% sometimes |
||
| 118 | var uploadedPercentage = Math.min(100, parseInt(100.0 * evt.loaded / evt.total)); |
||
| 119 | uploadInfoRecord.uploadProgress = uploadedPercentage; |
||
| 120 | }); |
||
| 121 | |||
| 122 | return uploadAction; |
||
| 123 | |||
| 124 | |||
| 125 | // if(typeof callback == "function") |
||
| 126 | // callback($scope.uploadInfo[recordCount]); |
||
| 127 | } |
||
| 128 | |||
| 129 | $scope.UploadData = function(files){ |
||
| 130 | // console.dir(files) |
||
| 131 | UploadFileList(files); |
||
| 132 | } |
||
| 133 | |||
| 134 | $scope.Initialize = function(){ |
||
| 135 | $scope.InitScope(); |
||
| 136 | if(typeof $scope.EventListener == "function"){ |
||
| 137 | $scope.EventListener($scope, $element, $attrs, $ctrl); |
||
| 138 | }else{ |
||
| 139 | EventListener(); |
||
| 140 | } |
||
| 141 | TryToCallInitDirective(); |
||
| 142 | } |
||
| 143 | $scope.InitScope = function(){ |
||
| 144 | InitializeUpload(); |
||
| 145 | } |
||
| 146 | |||
| 147 | function InitDirective(){ |
||
| 148 | if(Core.GetConfig().debugLog.DirectiveFlow) |
||
| 149 | console.log("scope.$id:"+$scope.$id+", may implement $scope.InitDirective() function in webapge"); |
||
| 150 | } |
||
| 151 | function EventListener(){ |
||
| 152 | if(Core.GetConfig().debugLog.DirectiveFlow) |
||
| 153 | console.log("scope.$id:"+$scope.$id+", may implement $scope.EventListener() function in webapge"); |
||
| 154 | } |
||
| 155 | // function SetDefaultValue(){ |
||
| 156 | // console.log("scope.$id:"+$scope.$id+", may implement $scope.SetDefaultValue() function in webapge"); |
||
| 157 | // } |
||
| 158 | function StatusChange(){ |
||
| 159 | if(Core.GetConfig().debugLog.DirectiveFlow) |
||
| 160 | console.log("scope.$id:"+$scope.$id+", may implement $scope.StatusChange() function in webapge"); |
||
| 161 | } |
||
| 162 | |||
| 163 | // $scope.Initialize(); |
||
| 164 | } |
||
| 165 | function templateFunction(tElement, tAttrs) { |
||
| 212 |